package com.jackrain.nea.fc.ps.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuwenxue
 * @date 2018/05/05
 **/
@Mapper
public interface ViewCProDistribToJDMapper {

    /**
     * 根据传入json条件查询主表记录
     *
     * @param id
     * @return List
     */
    @SelectProvider(type = ViewCproDistribToJDSql.class, method = "findProLsit")
    List<HashMap> findAllPro(@Param("id") Object[] id);

    /**
     * 根据传入json条件查询sku表，查询出对应的商品
     *
     * @param id
     * @return List
     */
    @SelectProvider(type = ViewCproDistribToJDSql.class, method = "findSKULsit")
    List<HashMap> findSku(@Param("id") Object[] id);

    /**
     * 根据商品ID在查询起商品条码
     *
     * @param id 商品ID
     * @return 商品条码
     */
    //TODO
    @Select("SELECT ECODE,PS_C_SPEC3OBJ_ID,PS_C_SPEC1OBJ_ID,PS_C_SPEC2OBJ_ID FROM " +
            "PS_C_SKU WHERE PS_C_PRO_ID=#{id} AND CP_C_DISTRIB_ID>0")
    List<HashMap> selSkuList(@Param("id") Long id);

    /**
     * 查询品牌名称
     *
     * @param id
     * @return
     */
    @Select("SELECT ENAME FROM PS_C_BRAND WHERE ID=#{id}")
    String selBrandEname(Long id);

    /**
     * 查询供应商名称
     *
     * @param id
     * @return
     */
    @Select("SELECT ENAME FROM CP_C_SUPPLIER WHERE ID=#{id}")
    String selSupplierEname(Long id);

    /**
     * 查询大类名称
     *
     * @param id
     * @return
     */
    @Select("SELECT ENAME FROM PS_C_PRODIM_ITEM WHERE ID=#{id}")
    String selLargeClassEname(Long id);

    /**
     * 查询商品规格颜色尺码
     *
     * @param id
     * @return
     */
    @Select("SELECT ENAME FROM PS_C_SPECOBJ WHERE ID=#{id}")
    String selSpecobj(Long id);

    /**
     * 外键关联配销中心表CP_C_STOREORG
     *
     * @param id
     * @return String
     */
    @Select("select ECODE from CP_C_STOREORG where ID=#{id}")
    String findByDistrib(Long id);

    class ViewCproDistribToJDSql {

        public String findProLsit(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT("ID,ECODE,LARGECLASS,ENAME,CP_C_DISTRIB_ID,PS_C_BRAND_ID," +
                            "CP_C_SUPPLIER_ID,PRICELIST");
                    FROM("PS_C_PRO");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("CP_C_DISTRIB_ID > 0");
                }
            }.toString();
        }

        public String findSKULsit(Map<String, Object> map) {
            Object[] id = (Object[]) map.get("id");
            return new SQL() {
                {
                    SELECT("PS_C_PRO_ID,ECODE,PS_C_SPEC3OBJ_ID,PS_C_SPEC1OBJ_ID,PS_C_SPEC2OBJ_ID");
                    FROM("PS_C_SKU");
                    StringBuffer sb = new StringBuffer();
                    for (int i = 0; i < id.length; i++) {
                        sb.append(id[i] + ",");
                    }
                    sb.deleteCharAt(sb.length() - 1);
                    WHERE("ID IN (" + sb.toString() + ")");
                    WHERE("CP_C_DISTRIB_ID > 0");
                }
            }.toString();
        }


    }


}
